package com.bit.ThreadTest;


public class Test9 {
    //懒汉模式
   /* static class Singleton{
        private Singleton(){};
        private static Singleton instance = null;

        public static Singleton getInstance(){
            if (instance == null) {
                synchronized (Singleton.class) {
                    if (instance == null) {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }*/

    //wait
    static class WaitTask implements Runnable{
        Object lock = null;

        public WaitTask(Object lock){
            this.lock = lock;
        }

        @Override
        public void run() {

            synchronized(lock){
                System.out.println("wait开始");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("wait结束");
            }
        }
    }

    //notify
    static class NotifyTask implements Runnable{
        Object lock = null;
        public NotifyTask(Object lock){
            this.lock=lock;
        }
        @Override
        public void run() {
            synchronized(lock){
                System.out.println("notify开始");
                lock.notify();
                System.out.println("notify结束");
            }
        }
    }
    public static void main(String[] args) {
        Object lock = new Object();
        Thread t1 = new Thread(new WaitTask(lock));
        Thread t2 = new Thread(new NotifyTask(lock));
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();

    }
}
